package com.micro.ai.auth.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.micro.ai.auth.entity.User;
import org.apache.ibatis.annotations.*;

import java.util.List;

/**
 * 用户Mapper接口
 * 
 * @author micro-ai
 * @since 0.0.1
 */
@Mapper
public interface UserMapper extends BaseMapper<User> {

    /**
     * 根据租户ID和用户名查询用户
     * 
     * @param tenantId 租户ID
     * @param username 用户名
     * @return 用户信息
     */
    @Select("SELECT * FROM users WHERE tenant_id = #{tenantId} AND username = #{username} AND deleted_at IS NULL")
    User selectByTenantAndUsername(@Param("tenantId") String tenantId, @Param("username") String username);

    /**
     * 根据租户ID和邮箱查询用户
     * 
     * @param tenantId 租户ID
     * @param email 邮箱
     * @return 用户信息
     */
    @Select("SELECT * FROM users WHERE tenant_id = #{tenantId} AND email = #{email} AND deleted_at IS NULL")
    User selectByTenantAndEmail(@Param("tenantId") String tenantId, @Param("email") String email);

    /**
     * 根据用户名查询用户（跨租户）
     * 
     * @param username 用户名
     * @return 用户信息
     */
    @Select("SELECT * FROM users WHERE username = #{username} AND deleted_at IS NULL LIMIT 1")
    User selectByUsername(@Param("username") String username);

    /**
     * 根据邮箱查询用户（跨租户）
     * 
     * @param email 邮箱
     * @return 用户信息
     */
    @Select("SELECT * FROM users WHERE email = #{email} AND deleted_at IS NULL LIMIT 1")
    User selectByEmail(@Param("email") String email);

    /**
     * 根据手机号查询用户
     * 
     * @param phone 手机号
     * @return 用户信息
     */
    @Select("SELECT * FROM users WHERE phone = #{phone} AND deleted_at IS NULL")
    User selectByPhone(@Param("phone") String phone);

    /**
     * 根据租户ID和手机号查询用户
     * 
     * @param tenantId 租户ID
     * @param phone 手机号
     * @return 用户信息
     */
    @Select("SELECT * FROM users WHERE tenant_id = #{tenantId} AND phone = #{phone} AND deleted_at IS NULL")
    User selectByTenantAndPhone(@Param("tenantId") String tenantId, @Param("phone") String phone);

    /**
     * 删除用户所有角色
     */
    @Delete("DELETE FROM user_roles WHERE user_id = #{userId}")
    void deleteUserRoles(@Param("userId") String userId);

    /**
     * 批量插入用户角色
     */
    @Insert("<script>" +
            "INSERT INTO user_roles (user_id, role_id, created_at) VALUES " +
            "<foreach collection='roleIds' item='roleId' separator=','>" +
            "(#{userId}, #{roleId}, NOW())" +
            "</foreach>" +
            "</script>")
    void insertUserRoles(@Param("userId") String userId, @Param("roleIds") List<String> roleIds);

    /**
     * 删除用户指定角色
     */
    @Delete("<script>" +
            "DELETE FROM user_roles WHERE user_id = #{userId} AND role_id IN " +
            "<foreach collection='roleIds' item='roleId' open='(' separator=',' close=')'>" +
            "#{roleId}" +
            "</foreach>" +
            "</script>")
    void deleteUserRolesByRoleIds(@Param("userId") String userId, @Param("roleIds") List<String> roleIds);
}

